<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/all.conf.php');
require_once(__PHY_WEB.__DIR_ADM.'/admin.inc.php');
require_once(__PHY_LIB.'/class/FnOrder.class.php');
require_once(__PHY_LIB.'/class/uUser.class.php');
require_once(__PHY_LIB.'/class/uWork.class.php');
require_once(__PHY_LIB.'/func/Payment.func.php');

$rReq = validate($_REQUEST, 'msg:u|action:e[list,edit,update]');
$sMsg = $rReq['msg'];
$sAction = $rReq['action'];
$_br = "<br />\n";

if ('update' == $sAction) {
	do {
		$oOne = new Order($_REQUEST['id']);
		$rP = validate($_POST, 'status:u|ship_sn:u');
		if (!$rP['ship_sn']) { $sMsg .= "请输入物流发货单号".$_br; break; }
		if ('shipping' == $rP['status'] &&
			('prepaid' == $oOne->get('status') || 'paid' == $oOne->get('status'))) {
			$sOldStatus = $oOne->get('status');
			$sNewStatus = 'paid' == $sOldStatus ? 'closed' : $rP['status'];
			/// 保存 ///
			$oOne->set('ship_sn', $rP['ship_sn']);
			$oOne->set('status', $sNewStatus);
			$oOne->update();
			$sMsg .= "订单状态修改完毕。从{$sOldStatus}改为{$sNewStatus}。".$_br;
			/// 订单更新日志 ///
			$rParam = array(
				'ShipMethod' => $oOne->get('payment_method'),
				'ShipSn' => $oOne->get('ship_sn'),
			);
			$oOne->addChangelog('ship', $rParam);
			/// 同步 ///
			$sSendGoodsPhp = isset($_Payments[$oOne->get('payment_id')]['SendGoodsPhp'])
				? $_Payments[$oOne->get('payment_id')]['SendGoodsPhp'] : false;
			$sSendGoodsFunc = isset($_Payments[$oOne->get('payment_id')]['SendGoodsFunc'])
				? $_Payments[$oOne->get('payment_id')]['SendGoodsFunc'] : false;
			if ($sSendGoodsPhp && $sSendGoodsFunc) {
				include_once(__PHY_WEB.$sSendGoodsPhp);
				$rParam = array(
					'trade_no'       => $oOne->get('payment_sn'), // 交易号
					'logistics_name' => $oOne->get('ship_method'), // 物流公司名称
					'invoice_no'     => $oOne->get('ship_sn'), // 物流发货单号
					'transport_type' => 'EXPRESS', // 物流发货运输类型，本网站统一为EXPRESS（快递）
				);
				if ($sSendGoodsFunc($rParam)) {
					$sMsg .= "已送货信息已同步至".$oOne->get('payment_method').$_br;
				}
				else {
					$sMsg .= "已送货信息无法同步".$_br;
				}
			}
			else {
				$sMsg .= "该付款方式（".$oOne->get('payment_method')."）未设置物流同步方式".$_br;
			}
		}
		else {
			$sMsg .= "订单状态无变化。".$_br;
		}
	} while (false);
	$sAction = 'edit';
}

if ('edit' == $sAction) {
	$oOne = new Order($_REQUEST['id']);
	$rChangelog = $oOne->getChangelog();
	if ('user' == $oOne->get('owner_type')) {
		$oUser = new uUser($oOne->get('owner_id'), 'email,nick');
		$oOne->set('owner_email', $oUser->get('email'));
		$oOne->set('owner_nick', $oUser->get('nick'));
	}
	else {
		$oOne->set('owner_nick', '游客');
	}
	$oTpl->assign('rOne', $oOne->getData());
	$oTpl->assign('rChangelog', $rChangelog);

	/// 商品列表 ///
	$oList = new uWorkList("fnnc_order_work ow"
		. " INNER JOIN unit_work w ON w.id=ow.work_id"
		. " INNER JOIN unit_work_size s ON s.id=ow.size_id");
	$oList->cfgField("w.*,ow.size_id,ow.kuang_id,ow.quantity,s.title AS size_title,s.title_en AS size_title_en");
	$oList->cfgWhere("ow.order_id='{$_REQUEST['id']}'");
	$oList->cfgOrder("ow.id");
	$oList->load();
	$oTpl->assign('rWorks', $oList->fetchArray());
}
elseif ('list' == $sAction) {
	$rP = validate($_GET, 'status:e[all,unpaid,canceled,paid,shipping,closed,hided]|pg:n');
	$oTpl->assign('rP', $rP);
	$rPager = array(
		'Sze' => 50,
		'Req' => 'pg',
		'Num' => max(1, $rP['pg']),
	);
	$oList = new OrderList();
	if ('all' != $rP['status']) {
		$oList->cfgWhere("status='{$rP['status']}'");
	}
	$rPager['Cnt'] = $oList->countAll();
	$rPager['Max'] = ceil($rPager['Cnt']/$rPager['Sze']);
	$oList->cfgOrder("id DESC");
	$oList->load($rPager['Sze']*($rPager['Num'] - 1), $rPager['Sze']);
	while ($oOne = $oList->nextItem()) {
		$r = $oOne->getChangelog('prepay');
		$oOne->set('time_paid', $r['time_changed']);
		if ('user' == $oOne->get('owner_type')) {
			$oUser = new uUser($oOne->get('owner_id'), 'email,nick');
			$oOne->set('owner_nick', $oUser->get('nick'));
		}
		else {
			$oOne->set('owner_nick', '游客');
		}
	}
	$oTpl->assign('rList', $oList->fetchArray());
	$oTpl->assign('rPager', $rPager);
}

$oTpl->assign('sMsg', $sMsg);
$oTpl->assign('sAction', $sAction);
$oTpl->display(dirname(__FILE__).'/'.basename(__FILE__, '.php').'.tpl');
?>
